﻿/*
 * Copyright (C) 2011 Kamran Zafar
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */
 
/*
 * Created by SharpDevelop.
 * User: Kamran
 * Date: 27/01/2010
 * Time: 19:53
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace org.kamranzafar.hotfusion.gui
{
	/// <summary>
	/// Description of IconExtractorForm.
	/// </summary>
	public partial class IconExtractorForm : Form
	{
		public IconExtractorForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
			toolTip1.SetToolTip(largeIcon, "Click to save");
			toolTip1.SetToolTip(smallIcon, "Click to save");
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			if (DialogResult.OK != openFileDialog1.ShowDialog(this))
				return;

			fileTxt.Text = openFileDialog1.FileName;
			
			Icon li=IconExtractor.ExtractLargeIcon(fileTxt.Text);
			Icon si =IconExtractor.ExtractSmallIcon(fileTxt.Text);
			
			largeIcon.Image = li.ToBitmap();
			smallIcon.Image = si.ToBitmap();
		}
		
		void OkBtnClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void SmallIconClick(object sender, EventArgs e)
		{
			if(fileTxt.Text.Trim().Equals(""))
				return;
			
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				save(saveFileDialog1.FileName, smallIcon.Image);
				
				MessageBox.Show("Image Saved");
			}
		}
		
		void LargeIconClick(object sender, EventArgs e)
		{
			if(fileTxt.Text.Trim().Equals(""))
				return;
			
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				save(saveFileDialog1.FileName, largeIcon.Image);
				
				MessageBox.Show("Image Saved");
			}
		}
		
		private void save(string filename, Image img){
			if (filename.ToLower().EndsWith(".bmp"))
				img.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
			else if (filename.ToLower().EndsWith(".png"))
				img.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
			else if (filename.ToLower().EndsWith(".gif"))
				img.Save(filename, System.Drawing.Imaging.ImageFormat.Gif);
			else if (filename.ToLower().EndsWith(".jpg"))
				img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
			else if (filename.ToLower().EndsWith(".ico"))
				img.Save(filename, System.Drawing.Imaging.ImageFormat.Icon);
		}
	}
}
